package qc.module.cms.api.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import qc.common.core.utils.DateUtil;
import qc.module.cms.dto.leddisplay.*;
import qc.module.cms.dto.ledview.LedViewDto;
import qc.module.cms.dto.swipper.SwipperDto;
import qc.module.cms.service.CategorySwipperService;
import qc.module.cms.service.LedViewService;
import qc.module.cms.service.MonitorDataService;

import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * LED显示控制器
 *
 * @author QuCheng Tech
 * @since 2023/12/20
 */
@RestController
@RequestMapping("/leddisplay")
public class LedDisplayController {
    @Autowired
    private RestTemplate restTemplate;

    private MonitorDataService monitorDataService;

    private LedViewService ledViewService;

    private CategorySwipperService categorySwipperService;

    @Autowired
    public void setMonitorDataService(MonitorDataService monitorDataService) {
        this.monitorDataService = monitorDataService;
    }

    @Autowired
    public void setLedViewService(LedViewService ledViewService) {
        this.ledViewService = ledViewService;
    }

    @Autowired
    public void setCategorySwipperService(CategorySwipperService categorySwipperService) {
        this.categorySwipperService = categorySwipperService;
    }

    /**
     * 获取大屏显示配置信息，可以指定传入日期
     *
     * @param id 传入日期，为空时使用当天
     * @return qc.module.cms.dto.leddisplay.LedDisplayDto
     * @author QuCheng Tech
     * @since 2023/12/20
     */
    @RequestMapping(value = "/config", method = {RequestMethod.GET})
    public LedDisplayDto getConfigByParam(@RequestParam(value = "id", required = false) String id) {
        return getLedDisplayConfig(id);
    }

    /**
     * 获取大屏显示配置信息，可以指定传入日期
     *
     * @param id 传入日期，为空时使用当天
     * @return qc.module.cms.dto.leddisplay.LedDisplayDto
     * @author QuCheng Tech
     * @since 2023/12/20
     */
    @RequestMapping(value = "/config/{id}", method = {RequestMethod.GET})
    public LedDisplayDto getConfigByPath(@PathVariable(required = false) String id) {
        return getLedDisplayConfig(id);
    }

    /**
     * 获取指定时间（如未传入时为当前时间）LED显示配置；
     * LED显示的画面类型有：网站轮播图、负荷曲线、固定配置的画面
     * 处理顺序如下：
     * 1.如配置有固定显示的画面（1个或多个），只在配置的固定画面中进行轮播；不显示网站轮播图和负荷曲线；
     * 2.如配置有显示的画面（不是固定显示，1个或多个），在网站轮播、负荷曲线、画面之间进行轮换
     *
     * @param dateString 时间字符串
     * @return qc.module.cms.dto.leddisplay.LedDisplayDto
     * @author QuCheng Tech
     * @since 2023/12/20
     */
    LedDisplayDto getLedDisplayConfig(String dateString) {
        //默认为当前时间
        Date date = DateUtil.getNowDate();
        if (!StringUtils.isBlank(dateString)) {
            //如果有输入日期进行转换，转换失败不进行异常处理
            Date d = DateUtil.parseDate(dateString);
            if (d != null)
                date = d;
        }

        LedDisplayDto result = new LedDisplayDto();
        result.setDynamo(true);

        //先进行画面的查询，如果有固定显示的画面，不需要查询网站轮播图信息
        boolean hasFixedLedViews = false;
        List<LedViewDto> ledViews = ledViewService.getDisplayViews(date);
        if (ledViews != null && ledViews.size() > 0x0) {
            //判断是否有固定的画面，有固定的画面时只显示固定的画面
            List<LedViewDto> fixedLedViews = ledViews.stream().filter(p -> p.isFixed() == true).collect(Collectors.toList());
            if (fixedLedViews != null && fixedLedViews.size() > 0x0) {
                //有固定显示的画面
                hasFixedLedViews = true;
                //设置机组负荷曲线不显示
                result.setDynamo(false);
                //显示画面仅为固定画面
                result.setViews(fixedLedViews);
            } else {
                //没有固定画面，所有配置的画面均要返回
                result.setViews(ledViews);
            }
        }

        if (!hasFixedLedViews) {
            //没有固定显示的画面，查询网站轮播图
            List<SwipperDto> swippers = categorySwipperService.getCategoryValidSwippers(0x1, 0x5);
            if (swippers != null && swippers.size() > 0x0)
                result.setSwippers(swippers);
        }

        return result;
    }

    /**
     * 获取电厂实时数据，可以指定传入日期
     *
     * @param id 传入日期，为空时使用当天
     * @return qc.module.cms.dto.leddisplay.PlantRealDataDto
     * @author QuCheng Tech
     * @since 2023/12/20
     */
    @RequestMapping(value = "/dynamo", method = {RequestMethod.GET})
    public PlantRealDataDto getDynamoRealDatasByParam(@RequestParam(value = "id", required = false) String id) {
        return getDynamoRealDatas(id);
    }

    /**
     * 获取电厂实时数据，可以指定传入日期
     *
     * @param id 传入日期，为空时使用当天
     * @return qc.module.cms.dto.leddisplay.PlantRealDataDto
     * @author QuCheng Tech
     * @since 2023/12/20
     */
    @RequestMapping(value = "/dynamo/{id}", method = {RequestMethod.GET})
    public PlantRealDataDto getDynamoRealDatasByPath(@PathVariable(required = false) String id) {
        return getDynamoRealDatas(id);
    }

    /**
     * 根据传入的日期字符串进行电厂实时数据查询，如果没有传入日期字符串或传入错误默认使用当天时间
     *
     * @param dateString 日期字符串
     * @return qc.module.cms.dto.leddisplay.PlantRealDataDto
     * @author QuCheng Tech
     * @since 2023/12/20
     */
    PlantRealDataDto getDynamoRealDatas(String dateString) {
        //日期默认为当天
        Date date = DateUtil.getTodayDate();
        if (!StringUtils.isBlank(dateString)) {
            //如果有输入日期进行转换，转换失败不进行异常处理
            Date d = DateUtil.parseDate(dateString);
            if (d != null)
                date = d;
        }

        //从mysql数据库中查询机组数据，需要使用软件接入数据并保存在数据库
        //return getKaiYuanDynamoRealDatas(date);
        //从接口模拟数据
        return getKaiYuanSimularDynamoRealDatas(date);
        //从开远SIS系统提供的接口中获取数据
        //return getKaiYuanSisDynamoRealDatas(date);
    }

    /**
     * 查询开远公司指定日期的机组数据，在方法中固定站点、数据类型等信息
     *
     * @param date 日期
     * @return qc.module.cms.dto.leddisplay.PlantRealDataDto
     * @author QuCheng Tech
     * @since 2023/12/20
     */
    PlantRealDataDto getKaiYuanDynamoRealDatas(Date date) {
        //固定查询条件信息
        String eng1Stcd = "eng1";
        String eng2Stcd = "eng2";
        String mwDatatype = "mw";
        String so2Datatype = "so2";
        String noxDatatype = "nox";

        //模拟数据时自动使用当前日期和当前时间
        Date now = DateUtil.getNowDate();
        //直接对传入的日期参数赋值修改，后面统一计算一天的开始和结束时间
        date = now;

        //查询时间段，使用指定日期的0点和24点
        Date beginTime = DateUtil.getDayDate(date);
        Date endTime = DateUtil.addDays(beginTime, 0x1);
        System.out.println("beginTime=" + beginTime.toString() + " ,endTime=" + endTime.toString());

        //构造查询结果
        PlantRealDataDto result = new PlantRealDataDto();
        result.setBeginTime(DateUtil.getDateTimeString(beginTime));
        result.setEndTime(DateUtil.getDateTimeString(endTime));
        //定义机组数据和机组信息集合
        List<DynamoRealDataDto> datas = new ArrayList<>();
        List<String> infos = new ArrayList<>();

        //第1台机组数据
        DynamoInfoDto dynamo1Info = new DynamoInfoDto();
        dynamo1Info.setName("7#机组");
        dynamo1Info.setCapacity(300.0f);
        //转换为Json字符串
        infos.add(JSON.toJSONString(dynamo1Info));
        DynamoRealDataDto dynamo1Data = new DynamoRealDataDto();

        //机组mw数据
        Map<Date, Double> dynamoMw = monitorDataService.getMonitorDatas(eng1Stcd, mwDatatype, beginTime, endTime, false);
        if (dynamoMw != null && dynamoMw.size() > 0x0) {
            List<MonitorDataDto> mws = new ArrayList<>();
            for (Map.Entry<Date, Double> mw : dynamoMw.entrySet()) {
                MonitorDataDto mwDto = new MonitorDataDto();
                mwDto.setTm(DateUtil.getDateTimeString(mw.getKey()));
                mwDto.setValue(mw.getValue());

                mws.add(mwDto);
            }
            dynamo1Data.setMw(mws);
        }
        //机组so2数据
        Map<Date, Double> dynamoSo2 = monitorDataService.getMonitorDatas(eng1Stcd, so2Datatype, beginTime, endTime, false);
        if (dynamoSo2 != null && dynamoSo2.size() > 0x0) {
            List<MonitorDataDto> so2s = new ArrayList<>();
            for (Map.Entry<Date, Double> so2 : dynamoSo2.entrySet()) {
                MonitorDataDto so2Dto = new MonitorDataDto();
                so2Dto.setTm(DateUtil.getDateTimeString(so2.getKey()));
                so2Dto.setValue(so2.getValue());

                so2s.add(so2Dto);
            }
            dynamo1Data.setSo2(so2s);
        }
        //机组nox数据
        Map<Date, Double> dynamoNox = monitorDataService.getMonitorDatas(eng1Stcd, noxDatatype, beginTime, endTime, false);
        if (dynamoNox != null && dynamoNox.size() > 0x0) {
            List<MonitorDataDto> noxs = new ArrayList<>();
            for (Map.Entry<Date, Double> nox : dynamoNox.entrySet()) {
                MonitorDataDto noxDto = new MonitorDataDto();
                noxDto.setTm(DateUtil.getDateTimeString(nox.getKey()));
                noxDto.setValue(nox.getValue());

                noxs.add(noxDto);
            }
            dynamo1Data.setNox(noxs);
        }
        datas.add(dynamo1Data);

        //第2台机组数据
        DynamoInfoDto dynamo2Info = new DynamoInfoDto();
        dynamo2Info.setName("8#机组");
        dynamo2Info.setCapacity(300.0f);
        //转换为Json字符串
        infos.add(JSON.toJSONString(dynamo2Info));
        DynamoRealDataDto dynamo2Data = new DynamoRealDataDto();
        //机组mw数据
        dynamoMw = monitorDataService.getMonitorDatas(eng2Stcd, mwDatatype, beginTime, endTime, false);
        if (dynamoMw != null && dynamoMw.size() > 0x0) {
            List<MonitorDataDto> mws = new ArrayList<>();
            for (Map.Entry<Date, Double> mw : dynamoMw.entrySet()) {
                MonitorDataDto mwDto = new MonitorDataDto();
                mwDto.setTm(DateUtil.getDateTimeString(mw.getKey()));
                mwDto.setValue(mw.getValue());

                mws.add(mwDto);
            }
            dynamo2Data.setMw(mws);
        }
        //机组so2数据
        dynamoSo2 = monitorDataService.getMonitorDatas(eng2Stcd, so2Datatype, beginTime, endTime, false);
        if (dynamoSo2 != null && dynamoSo2.size() > 0x0) {
            List<MonitorDataDto> so2s = new ArrayList<>();
            for (Map.Entry<Date, Double> so2 : dynamoSo2.entrySet()) {
                MonitorDataDto so2Dto = new MonitorDataDto();
                so2Dto.setTm(DateUtil.getDateTimeString(so2.getKey()));
                so2Dto.setValue(so2.getValue());

                so2s.add(so2Dto);
            }
            dynamo2Data.setSo2(so2s);
        }
        //机组nox数据
        dynamoNox = monitorDataService.getMonitorDatas(eng2Stcd, noxDatatype, beginTime, endTime, false);
        if (dynamoNox != null && dynamoNox.size() > 0x0) {
            List<MonitorDataDto> noxs = new ArrayList<>();
            for (Map.Entry<Date, Double> nox : dynamoNox.entrySet()) {
                MonitorDataDto noxDto = new MonitorDataDto();
                noxDto.setTm(DateUtil.getDateTimeString(nox.getKey()));
                noxDto.setValue(nox.getValue());

                noxs.add(noxDto);
            }
            dynamo2Data.setNox(noxs);
        }
        datas.add(dynamo2Data);

        result.setDatas(datas);
        result.setInfos(infos);

        return result;
    }

    /**
     * 查询开远公司模拟机组数据
     *
     * @param date 日期
     * @return qc.module.cms.dto.leddisplay.PlantRealDataDto
     * @author QuCheng Tech
     * @since 2023/12/20
     */
    PlantRealDataDto getKaiYuanSimularDynamoRealDatas(Date date) {
        //固定查询条件信息
        String eng1Stcd = "eng1";
        String eng2Stcd = "eng2";
        String mwDatatype = "mw";
        String so2Datatype = "so2";
        String noxDatatype = "nox";

        //模拟数据时自动使用当前日期和当前时间
        Date now = DateUtil.getNowDate();
        //直接对传入的日期参数赋值修改，后面统一计算一天的开始和结束时间
        date = now;

        //查询时间段，使用指定日期的0点和24点
        Date beginTime = DateUtil.getDayDate(date);
        Date endTime = DateUtil.addDays(beginTime, 0x1);
        System.out.println("beginTime=" + beginTime.toString() + " ,endTime=" + endTime.toString());

        //构造查询结果
        PlantRealDataDto result = new PlantRealDataDto();
        result.setBeginTime(DateUtil.getDateTimeString(beginTime));
        result.setEndTime(DateUtil.getDateTimeString(endTime));
        //定义机组数据和机组信息集合
        List<DynamoRealDataDto> datas = new ArrayList<>();
        List<String> infos = new ArrayList<>();

        //第1台机组数据
        DynamoInfoDto dynamo1Info = new DynamoInfoDto();
        dynamo1Info.setName("7#机组");
        dynamo1Info.setCapacity(300.0f);
        //转换为Json字符串
        infos.add(JSON.toJSONString(dynamo1Info));
        DynamoRealDataDto dynamo1Data = new DynamoRealDataDto();

//机组mw数据
        dynamo1Data.setMw(simularTimeSeriesData(beginTime, now, 300, 280.0d, 30.0d));
        //机组so2数据
        dynamo1Data.setSo2(simularTimeSeriesData(beginTime, now, 300, 30.0d, 5.0d));
        //机组nox数据
        dynamo1Data.setNox(simularTimeSeriesData(beginTime, now, 300, 110.0d, 10.0d));
        datas.add(dynamo1Data);

        //第2台机组数据
        DynamoInfoDto dynamo2Info = new DynamoInfoDto();
        dynamo2Info.setName("8#机组");
        dynamo2Info.setCapacity(300.0f);
        //转换为Json字符串
        infos.add(JSON.toJSONString(dynamo2Info));
        DynamoRealDataDto dynamo2Data = new DynamoRealDataDto();
        //机组mw数据
        dynamo2Data.setMw(simularTimeSeriesData(beginTime, now, 300, 280.0d, 30.0d));
        //机组so2数据
        dynamo2Data.setSo2(simularTimeSeriesData(beginTime, now, 300, 30.0d, 5.0d));
        //机组nox数据
        dynamo2Data.setNox(simularTimeSeriesData(beginTime, now, 300, 110.0d, 10.0d));
        datas.add(dynamo2Data);

        result.setDatas(datas);
        result.setInfos(infos);

        return result;
    }

    //用于生成时间序列的模拟数据
    List<MonitorDataDto> simularTimeSeriesData(Date beginTime, Date endTime, int intervalSeconds, Double baseValue, Double shiftValue) {
        List<MonitorDataDto> result = new ArrayList<>();
        Date time = beginTime;
        while (time.before(endTime)) {
            double randomValue = Math.random();
            //如果随机数＞0.5，使用减法：1-随机数
            double value = baseValue + shiftValue * (randomValue > 0.5 ? (1 - randomValue) : randomValue);

            MonitorDataDto dto = new MonitorDataDto();
            dto.setTm(DateUtil.getDateTimeString(time));
            dto.setValue(value);

            result.add(dto);

            time = DateUtil.addSeconds(time, intervalSeconds);
        }
        return result;
    }

    /**
     * 查询开远公司机组数据--来源于SIS的WebService接口
     *
     * @param date 日期
     * @return qc.module.cms.dto.leddisplay.PlantRealDataDto
     * @author QuCheng Tech
     * @since 2023/12/20
     */
    PlantRealDataDto getKaiYuanSisDynamoRealDatas(Date date) {
        //固定查询条件信息
        String eng1Stcd = "eng1";
        String eng2Stcd = "eng2";
        String mwDatatype = "mw";
        String so2Datatype = "so2";
        String noxDatatype = "nox";

        //模拟数据时自动使用当前日期和当前时间
        Date now = DateUtil.getNowDate();
        //直接对传入的日期参数赋值修改，后面统一计算一天的开始和结束时间
        date = now;

        //查询时间段，使用指定日期的0点和24点
        Date beginTime = DateUtil.getDayDate(date);
        Date endTime = DateUtil.addDays(beginTime, 0x1);
        System.out.println("beginTime=" + beginTime.toString() + " ,endTime=" + endTime.toString());

        //构造查询结果
        PlantRealDataDto result = new PlantRealDataDto();
        result.setBeginTime(DateUtil.getDateTimeString(beginTime));
        result.setEndTime(DateUtil.getDateTimeString(endTime));
        //定义机组数据和机组信息集合
        List<DynamoRealDataDto> datas = new ArrayList<>();
        List<String> infos = new ArrayList<>();

        //从SIS接口查询数据后进行赋值
        //使用顺序记录点号的实时值
        double[] realDataValues = new double[0x8];
        List<MonitorDataDto> dynamo1MwDatas = new ArrayList<>();
        List<MonitorDataDto> dynamo2MwDatas = new ArrayList<>();
        //查询实时数据接口地址，其中包含全部点号
        //点号的顺序：7#机负荷、7#机NOX、7#机SO2、7#机粉尘、8#机负荷、8#机NOX、8#机SO2、8#机粉尘；结果返回的顺序与点号顺序一致
        String sisQueryRealDataUrl = "http://10.213.1.40:8888/KYSIS/queryRealData?taglist=SYNCBASE6.7XNJS07,SYNCBASE6.UNIT07_7YQFX-3,SYNCBASE6.UNIT07_7YQFX-2,SYNCBASE6.UNIT07_7YQFX-4,SYNCBASE6.8XNJS08,SYNCBASE6.UNIT08_8YQFX_3A,SYNCBASE6.UNIT08_8YQFX_2A,SYNCBASE6.UNIT08_8YQFX_4A";
        String responseQueryRealDataBodyString = GetHttpResponseBody(sisQueryRealDataUrl, HttpMethod.GET, null);
        JSONObject responseRealJsonObject = JSONObject.parseObject(responseQueryRealDataBodyString);
        if (responseRealJsonObject != null) {
            // 判断返回结果Json中的success为true
            if (responseRealJsonObject.getBoolean("success")) {
                //获取data，数组，按请求给的点号顺序
                JSONArray jsonArray = responseRealJsonObject.getJSONArray("data");
                int dataIndex = 0x0;
                for (Object quotaDataObject : jsonArray) {
                    //解析每个点号的数据
                    JSONObject quotaDataJsonObject = (JSONObject) quotaDataObject;
                    Date time = quotaDataJsonObject.getDate("time");
                    double value = quotaDataJsonObject.getDoubleValue("value");
                    
                    System.out.println("queryRealData dataIndex=" + dataIndex + " ,value=" + value);
                    realDataValues[dataIndex++] = value;
                }
            }
        }

        //点号的顺序：7#机负荷、8#机负荷；结果返回的顺序与点号顺序一致，查询时间间隔1800s=30min
        //String sisQueryHistoryDataUrl = "http://10.213.1.40:8888/KYSIS/queryHisData?taglist=SYNCBASE6.7XNJS07,SYNCBASE6.8XNJS08&btime=" + DateUtil.getDateTimeString(beginTime) + "&etime=" + DateUtil.getDateTimeString(endTime) + "&intervalsecs=1800";
        //修改为查询历史数据的时间间隔： 300s=5min
        String sisQueryHistoryDataUrl = "http://10.213.1.40:8888/KYSIS/queryHisData?taglist=SYNCBASE6.7XNJS07,SYNCBASE6.8XNJS08&btime=" + DateUtil.getDateTimeString(beginTime) + "&etime=" + DateUtil.getDateTimeString(endTime) + "&intervalsecs=300";
        String responseQueryHistoryDataBodyString = GetHttpResponseBody(sisQueryHistoryDataUrl, HttpMethod.GET, null);
        JSONObject responseHistoryJsonObject = JSONObject.parseObject(responseQueryHistoryDataBodyString);
        if (responseHistoryJsonObject != null) {
            // 判断返回结果Json中的success为true
            if (responseHistoryJsonObject.getBoolean("success")) {
                //获取data，不是数组，是以点号为key的json对象
                JSONObject dataJsonObject = responseHistoryJsonObject.getJSONObject("data");
                //7#机组负荷数据
                JSONArray jsonArray_7XNJS07 = dataJsonObject.getJSONArray("7XNJS07");
                for (Object quotaDataObject : jsonArray_7XNJS07) {
                    //解析每个点号的数据
                    JSONObject quotaDataJsonObject = (JSONObject) quotaDataObject;
                    Date time = quotaDataJsonObject.getDate("time");
                    double value = quotaDataJsonObject.getDoubleValue("value");

                    MonitorDataDto dto = new MonitorDataDto();
                    dto.setTm(DateUtil.getDateTimeString(time));
                    dto.setValue(value);

                    dynamo1MwDatas.add(dto);
                }

                //8#机组负荷数据
                JSONArray jsonArray_8XNJS08 = dataJsonObject.getJSONArray("8XNJS08");
                for (Object quotaDataObject : jsonArray_8XNJS08) {
                    //解析每个点号的数据
                    JSONObject quotaDataJsonObject = (JSONObject) quotaDataObject;
                    Date time = quotaDataJsonObject.getDate("time");
                    double value = quotaDataJsonObject.getDoubleValue("value");

                    MonitorDataDto dto = new MonitorDataDto();
                    dto.setTm(DateUtil.getDateTimeString(time));
                    dto.setValue(value);

                    dynamo2MwDatas.add(dto);
                }
            }
        }

        //第1台机组数据
        DynamoInfoDto dynamo1Info = new DynamoInfoDto();
        dynamo1Info.setName("7#机组");
        dynamo1Info.setCapacity(320.0f);
        //转换为Json字符串
        infos.add(JSON.toJSONString(dynamo1Info));
        DynamoRealDataDto dynamo1Data = new DynamoRealDataDto();

        //机组的实时数据只要最新1条，不管时间
        MonitorDataDto latestData = new MonitorDataDto();
        latestData.setTm(DateUtil.getDateTimeString(now));
        List<MonitorDataDto> latestDataList = new ArrayList<>();

//机组mw数据
        dynamo1Data.setMw(dynamo1MwDatas);
        //机组so2数据
        latestData = new MonitorDataDto();
        latestData.setTm(DateUtil.getDateTimeString(now));
        latestData.setValue(realDataValues[0x2]);
        latestDataList = new ArrayList<>();
        latestDataList.add(latestData);
        dynamo1Data.setSo2(latestDataList);
        //机组nox数据
        latestData = new MonitorDataDto();
        latestData.setTm(DateUtil.getDateTimeString(now));
        latestData.setValue(realDataValues[0x1]);
        latestDataList = new ArrayList<>();
        latestDataList.add(latestData);
        dynamo1Data.setNox(latestDataList);
        datas.add(dynamo1Data);

        //第2台机组数据
        DynamoInfoDto dynamo2Info = new DynamoInfoDto();
        dynamo2Info.setName("8#机组");
        dynamo2Info.setCapacity(320.0f);
        //转换为Json字符串
        infos.add(JSON.toJSONString(dynamo2Info));
        DynamoRealDataDto dynamo2Data = new DynamoRealDataDto();
        //机组mw数据
        dynamo2Data.setMw(dynamo2MwDatas);
        //机组so2数据
        latestData = new MonitorDataDto();
        latestData.setTm(DateUtil.getDateTimeString(now));
        latestData.setValue(realDataValues[0x6]);
        latestDataList = new ArrayList<>();
        latestDataList.add(latestData);
        dynamo2Data.setSo2(latestDataList);
        //机组nox数据
        latestData = new MonitorDataDto();
        latestData.setTm(DateUtil.getDateTimeString(now));
        latestData.setValue(realDataValues[0x5]);
        latestDataList = new ArrayList<>();
        latestDataList.add(latestData);
        dynamo2Data.setNox(latestDataList);
        datas.add(dynamo2Data);

        result.setDatas(datas);
        result.setInfos(infos);

        return result;
    }

    public String GetHttpResponseBody(String url, HttpMethod method, HttpEntity<String> formEntity) {
        //获取结果为json时设置header中的ContentType
        //HttpHeaders headers = new HttpHeaders();
        //headers.setContentType(MediaType.APPLICATION_JSON);
        ////
        //if (formEntity == null)
        //    formEntity = new HttpEntity<String>(headers);

        //设置编码
        restTemplate.getMessageConverters().set(0x1, new StringHttpMessageConverter(StandardCharsets.UTF_8));

        //url需要对特殊字符进行编码
        url = url.replace("%", "%25");
        url = url.replace("$", "%24");
        url = url.replace("#", "%23");
        url = url.replace("!", "%21");
        url = url.replace(" ", "%20");
        url = url.replace("+", "%2B");
        url = url.replace("@", "%40");
        //url = url.replace(":", "%3A");
        //url = url.replace("=", "%3D");
        //url = url.replace("?", "%3F");

        URI uri = URI.create(url);
        ResponseEntity<String> response = restTemplate.getForEntity(uri, String.class);
        //ResponseEntity<String> response = restTemplate.exchange(
        //        url,
        //        method,
        //        formEntity,
        //        String.class
        //);

        String responseBody = response.getBody();

        return responseBody;
    }
}
